﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ScreenTranslate
{
    public class Client
    {
        private Socket clientSocket;
        private Server server;
        private Message msg = new Message();

        public Client() { }
        public Client(Socket clientSocket, Server server)
        {
            this.clientSocket = clientSocket;
            this.server = server;
        }


        public void Close()
        {
            try
            {
                if (clientSocket != null)
                {
                    clientSocket.Close();
                }
                server.RemoveClient(this);
            }
            catch (Exception e)
            {
                Console.WriteLine("关闭Client异常:" + e);
            }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="bytes"></param>
        public void Send(byte[] bytes)
        {
            try
            {
                if (clientSocket != null && clientSocket.Connected)
                {
                    clientSocket.Send(bytes);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("发送数据异常:"+e);
                //关闭客户端
                Close();
            }
        }
    }
}
